//
// Created by Aron on 2024/11/2.
//

#include "off_protect.h"
#include "remote_control.h"
#include "Yaw.h"
#include "Elevator.h"
#include "Pitch.h"
#include "CAN_receive.h"
#include "tim.h"
static int protect_flag=0;
static int count = 3;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    if (htim->Instance == TIM2) {
        // 超时，执行关控保护
        HAL_TIM_Base_Stop_IT(htim);
        HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
        HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);
        HAL_TIM_Base_Stop(&htim1);
//        rc_ctrl.rc.ch[0] = 0;      //!< Channel 0
//        rc_ctrl.rc.ch[1] = 0; //!< Channel 1
//        rc_ctrl.rc.ch[2] = 0;
//        rc_ctrl.rc.ch[3] = 0; //!< Channel 3
//        rc_ctrl.rc.s[0] = 3;                  //!< Switch left
//        rc_ctrl.rc.s[1] = 3;
        HAL_GPIO_WritePin(Pumps_GPIO_Port, Pumps_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(E_valve_S_GPIO_Port, E_valve_S_Pin, GPIO_PIN_RESET);
        HAL_GPIO_WritePin(E_valve_L_GPIO_Port, E_valve_L_Pin, GPIO_PIN_RESET);
        YawFlag = 1;
        PitchFlag = 1;
        ElevatorFlag = 1;
        protect_flag = 1;
        for (int i = 2; i < 5; i++){
            motor_measure[i].round_cnt=0;
        }
    }
}
void off_protect(void) {
    if (protect_flag) {
        count -= 1;
        CAN1_cmd(0, 0, 0, 0);
        CAN2_cmd(0);
        if (count == 0) {
            protect_flag = 0;
            count = 3;
        }
        HAL_Delay(10);
    }
}
